/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.api;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.AddMenuReq;
import org.cshoo.tattoo.authorize.api.dto.AddRootMenuReq;
import org.cshoo.tattoo.authorize.api.dto.UpdateMenuReq;
import org.cshoo.tattoo.authorize.app.MenuAppService;
import org.cshoo.tattoo.authorize.domain.po.Menu;
import org.cshoo.tattoo.embed.api.dto.ValidResultResp;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/menu")
@Slf4j
public class MenuController {

    @Resource
    private MenuAppService menuAppService;

    @RequestMapping(value = "/check/code", method = RequestMethod.GET)
    public ValidResultResp checkMenuCodeAlreadyExists(@RequestParam String code, @RequestParam Long appId) {
        log.info("code:{},appId:{}", code, appId);
        ValidResultResp result = new ValidResultResp();
        boolean exists = menuAppService.menuCodeExists(code, appId);
        result.setValid(!exists);
        return result;
    }

    @RequestMapping(value = "/addRootMenu", method = RequestMethod.POST)
    public Response<?> addRootMenu(@RequestBody AddRootMenuReq addRootMenuReq) {
        menuAppService.addRootMenu(addRootMenuReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/addChildMenu", method = RequestMethod.POST)
    public Response<?> addChildMenu(@RequestBody AddMenuReq addMenuReq) {
        log.info("addMenuReq:{}", addMenuReq);
        menuAppService.addMenu(addMenuReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Response<?> updateMenu(@RequestBody UpdateMenuReq updateMenuReq) {
        menuAppService.updateMenu(updateMenuReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Response<?> deleteMenu(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        menuAppService.deleteMenu(id);
        return Response.builder().build();
    }

    @RequestMapping(value = "/findRootMenuByApp/{appId}", method = RequestMethod.GET)
    public List<Menu> findRootMenuByApp(@PathVariable("appId") Long appId) {
        log.info("appId:{}", appId);
        return menuAppService.findRootMenuByApp(appId);
    }

    @RequestMapping(value = "/findMenuByApp/{appId}", method = RequestMethod.GET)
    public List<Menu> findMenuByApp(@PathVariable("appId") Long appId) {
        log.info("appId:{}", appId);
        return menuAppService.findMenuByApp(appId);
    }

    @RequestMapping(value = "/findMenusByParent/{parentId}", method = RequestMethod.GET)
    public List<Menu> findMenusByParentId(@PathVariable("parentId") Long menuId) {
        log.info("menuId:{}", menuId);
        return menuAppService.findMenusByParentId(menuId);
    }

    @RequestMapping(value = "/findByRoleId/{roleId}", method = RequestMethod.GET)
    public List<Menu> findByRoleId(@PathVariable("roleId") Long roleId) {
        log.info("roleId:{}", roleId);
        return menuAppService.findByRoleId(roleId);
    }
}
